<?php

namespace app\api\Logic\Notice;

use app\api\Logic\CommonLogic;
use app\api\model\CommentModel;
use app\api\model\NoticeModel;
use think\Db;

class NoticeLogic
{

    /**
     * 获取通知首页数据
     *
     * @param [type] $param 前端传入数据 user_id shop_id
     * @return void
     */
    public function getNoticeIndex($param)
    {
        $remind_where = [
            'a.user_id' => $param['user_id'],
            'a.user_type' => 2,
            'a.is_read' => 0,
            'a.deletetime' => null,
            'a.shop_id' => ['in',[0,$param['shop_id']]]
        ];
        $interact_where = [
            'a.to_reply_id' => $param['user_id'],
            'a.to_reply_type' => 2,
            'a.is_read' => 0,
            'a.deletetime' => null,
            'a.shop_id' => ['in',[0,$param['shop_id']]]
        ];
        $noticeModel = new NoticeModel();
        $commentModel = new CommentModel();
        // 提醒消息
        $remind = $noticeModel->alias('a')->where($remind_where)->order('createtime desc')->field('id,title,createtime')->find();
        $remind_num = $noticeModel->alias('a')->where($remind_where)->count();
        // 互动消息
        $interact = $commentModel->alias('a')->where($interact_where)->order('createtime desc')->field('id,content,createtime')->find();
        $interact_num = $commentModel->alias('a')->where($interact_where)->count();
        $remind = $remind?$remind:[];
        $interact = $interact?$interact:[];
        return ['remind_num'=>$remind_num,'remind'=>$remind,'interact_num'=>$interact_num,'interact'=>$interact];
    }

    /**
     * 获取通知列表
     *
     * @param [type] $param 前端传入数据 
     * @param [type] $type 数据类型 1=提醒消息，2=互动消息
     * @return void
     */
    public function getNoticeList($param,$type)
    {
        switch ($type) {
            case '1':
                $res = $this->getRemindDetail($param['user_id'],$param['shop_id'],$param['page'],$param['page_size']);
                break;
            case '2':
                $res = $this->getinteractDetail($param['user_id'],$param['shop_id'],$param['page'],$param['page_size']);
                break;            
        }
        return ['data'=>$res['data'],'num'=>$res['num']];

    }

    /**
     * 获取提醒消息列表
     *
     * @param [type] $user_id 用户id
     * @param [type] $shop_id 校区id
     * @param [type] $page    分页
     * @param [type] $limit   显示数量
     * @return void
     */
    public function getRemindDetail($user_id,$shop_id,$page,$limit)
    {
        $remind_where = [
            'a.user_id' => $user_id,
            'a.user_type' => 2,
            'a.is_read' => 0,
            'a.deletetime' => null,
            'a.shop_id' => ['in',[0,$shop_id]]
        ];
        $noticeModel = new NoticeModel();
        // 提醒消息
        $remind = $noticeModel
        ->alias('a')
        ->where($remind_where)
        ->order('createtime desc')
        ->field('id,shop_id,title,content,style,relation_id,createtime')
        ->limit(($page-1)*$limit,$limit)
        ->select();
        $remind_num = $noticeModel->alias('a')->where($remind_where)->count();
        $remind = $remind?$remind:[];
        return ['data'=>$remind,'num'=>$remind_num];
    }

    /**
     * 获取互动消息列表
     *
     * @param [type] $user_id 用户id
     * @param [type] $shop_id 校区id
     * @param [type] $page    分页
     * @param [type] $limit   显示数量
     * @return void
     */
    public function getInteractDetail($user_id,$shop_id,$page,$limit)
    {
        $commentModel = new CommentModel();
        $commonLogic = new CommonLogic();
        $interact_where = [
            'a.to_reply_id' => $user_id,
            'a.to_reply_type' => 2,
            'a.is_read' => 0,
            'a.deletetime' => null,
            'a.shop_id' => ['in',[0,$shop_id]]
        ];
        $interact = $commentModel
        ->alias('a')
        ->where($interact_where)
        ->join('article b',"a.relation_id = b.id and a.style='Article' ",'left') //文章
        ->join('interact c',"a.relation_id = c.id and a.style='Interact' ",'left')//互动讨论
        ->join('theme d',"c.theme_id = d.id and a.style='Interact' ",'left') //互动讨论主题
        ->join('comment e',"a.to_id = e.id",'left') //我发的评论
        ->order('a.createtime desc')
        ->field('a.id,a.shop_id,a.style,a.relation_id,a.reply_type,a.reply_id,a.content,a.createtime,a.style,b.title as article_title,b.type as article_type,c.title as interact_title,d.title as interact_theme,e.content as my_content')
        ->limit(($page-1)*$limit,$limit)
        ->select();
        $article_theme = [1=>'家长必读',2=>'热点资讯',3=>'学习资源',4=>'萌娃圈'];
        foreach ($interact as $v) {
            switch ($v['style']) {
                case 'Article'://文章：文章标题、文章类别
                    $v['title'] = $v['article_title'];
                    $v['theme'] = $article_theme[$v['article_type']];
                    break;
                case 'Interact'://互动讨论：贴子标题、贴子主题
                    $v['title'] = $v['interact_title'];
                    $v['theme'] = $v['interact_theme'];
                    break;               
            }
            unset($v['article_title']);
            unset($v['article_type']);
            unset($v['interact_title']);
            unset($v['interact_theme']);
            $user_info = $commonLogic->getUserInfo($v['reply_type'],$v['reply_id']);
            $v['nickname'] = $user_info['nickname'];
            $v['avatar'] = $user_info['avatar'];
        }

        $interact_num = $commentModel->alias('a')->where($interact_where)->count();
        $interact = $interact?$interact:[];
        return ['data'=>$interact,'num'=>$interact_num];
    }


    public function readNotice($ids,$user_id,$type)
    {
        $where = [];
        switch ($type) {
            case 'remind':
                $model = new NoticeModel();
                $where['user_id'] = $user_id;
                break;
            case 'interact':
                $model = new CommentModel();
                break;       
        }
        Db::startTrans();
        try{
            $model->where('id','in',$ids)->where($where)->update(['is_read'=>1]);
            // 提交事务
            Db::commit();    
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return false;
        }
        return true;
    }

}
